// FirstDiagPage.cpp : implementation file
//

#include "stdafx.h"
#include "../Frame/GYWalkAidApp.h"
#include "FirstDiagPage.h"
#include "../Gait/GaitAnaly.h"
#include "../Common/CommonDef.h"
#include "../Common/CommonFun.h"
#include "../Common/GYDataSet.h"
#include "PatMan.h"
#include "../Common/SmartWAParam.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFirstDiagPage dialog


CFirstDiagPage::CFirstDiagPage(CWnd* pParent /*=NULL*/)
	: CPageDialog(CFirstDiagPage::IDD, pParent)
{
	m_pGaitDraw		= NULL;
	memset(&m_BasicPara, 0, sizeof(m_BasicPara));

	ZERO_ARRAY(m_adwGaitSnap);
	m_nStartPos		= 0;
	m_dwStiOpen		= 0;
	m_dwStiClose	= 0;

	m_bTextChangeFromGait = FALSE;

	//{{AFX_DATA_INIT(CFirstDiagPage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CFirstDiagPage::~CFirstDiagPage()
{
	StopGait();
	GetApp()->SetGaitWindow(NULL);
}

void CFirstDiagPage::DoDataExchange(CDataExchange* pDX)
{
	CPageDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFirstDiagPage)
	DDX_Control(pDX, IDC_SPIN_OPEN_VALUE, m_spinOpenValue);
	DDX_Control(pDX, IDC_SPIN_CLOSE_VALUE, m_spinCloseValue);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFirstDiagPage, CPageDialog)
	//{{AFX_MSG_MAP(CFirstDiagPage)
	ON_BN_CLICKED(IDB_WRITEPARAM, OnBtnWriteParam)
	ON_BN_CLICKED(IDC_BUTTON_SNAP, OnButtonSnap)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_CLOSE_VALUE, OnDeltaposSpinCloseValue)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_OPEN_VALUE, OnDeltaposSpinOpenValue)
	ON_BN_CLICKED(IDC_BUTTON_CONTINUE, OnButtonContinue)
	ON_EN_CHANGE(IDC_EDIT_STI_OPEN, OnChangeEditStiOpen)
	ON_EN_CHANGE(IDC_EDIT_STI_CLOSE, OnChangeEditStiClose)
	//}}AFX_MSG_MAP
	ON_MESSAGE(GYWA_MSG_REALTIME_OPEN_CLOSE_VALUE, OnRefreshOpenClose)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFirstDiagPage message handlers

BOOL CFirstDiagPage::OnInitDialog() 
{
	CPageDialog::OnInitDialog();

	m_spinOpenValue.SetRange(MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE);
	m_spinOpenValue.SetBuddy(GetDlgItem(IDC_EDIT_STI_OPEN));	

	m_spinCloseValue.SetRange(MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE);
	m_spinCloseValue.SetBuddy(GetDlgItem(IDC_EDIT_STI_CLOSE));	
	
	CButton* pRadio = (CButton*)GetDlgItem(IDC_RADIO_MAN);
	pRadio->SetCheck(TRUE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CFirstDiagPage::OnBtnWriteParam() 
{
	DWORD dwMRNCode = GetApp()->GetCurSN();
	if(dwMRNCode == 0)
		return;
	
	CString strStiOpen;
	GetDlgItem(IDC_EDIT_STI_OPEN)->GetWindowText(strStiOpen);
	int nStiOpen = _ttoi(strStiOpen);

	CString strStiClose;
	GetDlgItem(IDC_EDIT_STI_CLOSE)->GetWindowText(strStiClose);
	int nStiClose = _ttoi(strStiClose);

	if(!IsNumInRange(nStiOpen, MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE) || !IsNumInRange(nStiClose, MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE))
	{
		CString strFormat = GetResString(IDS_PAT_OPEN_CLOSE_RANGE);
		CString strInfo;
		strInfo.Format(strFormat, MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE);

		GYMsgBox(strInfo, MB_OK|MB_ICONEXCLAMATION);
		return;
	}

#ifdef THIGH_WA_MODE
	if(nStiOpen >= nStiClose)
	{
		GYMsgBox(IDS_PAT_OPEN_LESS_CLOSE, MB_OK|MB_ICONEXCLAMATION);
		return;
	}
#else
	if(nStiClose >= nStiOpen)
	{
		GYMsgBox(IDS_PAT_CLOSE_LESS_OPEN, MB_OK|MB_ICONEXCLAMATION);
		return;
	}
#endif

	if(!CGaitAnaly::HasSdiagSnapGait(dwMRNCode, 0))
	{
		if(GYMsgBox(IDS_PAT_NO_GAIT, MB_YESNO|MB_ICONQUESTION) != IDYES)
			return;
	}

	if(GYMsgBox(IDS_PAT_CONFIRM_SAVE, MB_YESNO|MB_ICONQUESTION) != IDYES)
		return;

	CWaitCursor wait;
	StopGait();

	if(!WriteWAParams())
		return;

	if(SaveDiagRecord())
	{
		GYMsgBox(IDS_GBL_SAVE_SUCCESS, MB_ICONINFORMATION);
		EnableInputControls(FALSE);

		CPatMan* pParent = (CPatMan*)GetParent();
		//  Don't enable subsequent diagnose, it will be enabled by New Subsequent Diagnose in the query tab.
		pParent->EnableTab(TAB_INDEX_SDQUERY, TRUE);

		CPageDialog* pSDiagQuery = pParent->GetTabPage(TAB_INDEX_SDQUERY);
		if(pSDiagQuery)
			pSDiagQuery->ShowTableData(GetApp()->GetCurMRN());
	}
}

BOOL CFirstDiagPage::Prepare()
{
	GetApp()->SetGaitWindow(GetSafeHwnd());

	if(!IsControlsEnable())
	{
		SetTimer(GYWA_MSG_1STDIAG_TIMERID_SHOWGAIT, 100, ShowGaitTimerFunc);
	}
	else
	{
		//  if sdiag is working, stop the gait first
		CPatMan* pParent = (CPatMan*)GetParent();
		if(pParent)
		{
			CSubsqtDiagPage* pSDiag = (CSubsqtDiagPage*)pParent->GetTabPage(TAB_INDEX_SUBSDIAG);
			if(pSDiag)
				pSDiag->ClearUp();
		}
		
		if(m_pGaitDraw == NULL)
		{
			memset(&m_BasicPara, 0, sizeof(m_BasicPara));
			GetBasicParamToDisplay();

			m_pGaitDraw = new CGaitAnaly((CStatic*)GetDlgItem(IDC_STATIC_1STDIAG_GAIT), GetApp()->GetCurrentWAIndex());
			m_pGaitDraw->SetGaitPara(&m_BasicPara);
			m_pGaitDraw->StartDraw();

			GetDlgItem(IDC_STATIC_CONNECT_STATE)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_STATIC_FIRMWARE_VER)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_STATIC_FWVER)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_STATIC_CONNSTATE)->ShowWindow(SW_SHOW);
		}
		else
		{
			if(m_pGaitDraw->IsDrawPaused())
				SetTimer(GYWA_MSG_1STDIAG_TIMERID_SHOWGAIT, 100, ShowGaitTimerFunc);
		}
	}

	return TRUE;
}

BOOL CFirstDiagPage::ClearUp()
{
	return StopGait();
}

BOOL CFirstDiagPage::StopGait()
{
	if(m_pGaitDraw)
	{
		m_pGaitDraw->StopDraw();
		SAFE_DELETE(m_pGaitDraw);

		GetDlgItem(IDC_STATIC_CONNECT_STATE)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_STATIC_FIRMWARE_VER)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_STATIC_FWVER)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_STATIC_CONNSTATE)->ShowWindow(SW_HIDE);
	}
	
	return TRUE;
}

BOOL CFirstDiagPage::ClearData()
{
	GetDlgItem(IDC_EDIT_UP_TIME)->SetWindowText(_T(""));
	GetDlgItem(IDC_EDIT_DOWN_TIME)->SetWindowText(_T(""));
	GetDlgItem(IDC_EDIT_STI_OPEN)->SetWindowText(_T(""));
	GetDlgItem(IDC_EDIT_STI_CLOSE)->SetWindowText(_T(""));
	
	memset(&m_BasicPara, 0, sizeof(m_BasicPara));

	return TRUE;
}

BOOL CFirstDiagPage::WriteWAParams()
{
	CString strUpTime;
	CWnd* pUpTime = GetDlgItem(IDC_EDIT_UP_TIME);
	pUpTime->GetWindowText(strUpTime);
	
	CString strDownTime;
	CWnd* pDownTime = GetDlgItem(IDC_EDIT_DOWN_TIME);
	pDownTime->GetWindowText(strDownTime);
	
	CString strStiOpen;
	CWnd* pStiOpen = GetDlgItem(IDC_EDIT_STI_OPEN);
	pStiOpen->GetWindowText(strStiOpen);
	
	CString strStiClose;
	CWnd* pStiClose = GetDlgItem(IDC_EDIT_STI_CLOSE);
	pStiClose->GetWindowText(strStiClose);

	double dUpTime		= _tcstod(strUpTime, NULL);
	double dDownTime	= _tcstod(strDownTime, NULL);
	DWORD dwStiClose	= _ttoi(strStiClose);
	DWORD dwStiOpen		= _ttoi(strStiOpen);

	WA_BASIC_PARA wbp = {0};
	wbp.dwSize			= sizeof(WA_BASIC_PARA);
	wbp.dwPulseFrq		= m_BasicPara.dwPulseFrq;
	wbp.dwPulseWidth	= m_BasicPara.dwPulseWidth;
	wbp.dwPulseCount	= m_BasicPara.dwPulseCount;
	wbp.dwStiUpTime		= (dUpTime		* 100);
	wbp.dwStiDownTime	= (dDownTime	* 100);
	wbp.dwOpenThr		= (dwStiOpen	& 0xff);
	wbp.dwCloseThr		= (dwStiClose	& 0xff);

	CWAManager* pBTMan = GetApp()->GetWAMan();
	int nIndex = GetApp()->GetCurrentWAIndex();

	//  Maybe some bytes come from gait, sleep some time
	Sleep(300);

	BOOL bWBP = FALSE;
	WRITE_WA_MORE_TIMES(bWBP, pBTMan->cmdWriteParamOCD(nIndex, wbp));

	if(!bWBP)
	{
		GYMsgBox(IDS_SYS_WRITE_PARAM_ERROR, MB_ICONSTOP);
		return FALSE;
	}

	if(pBTMan->GetWACommVersion(nIndex) >= 0x12)
	{
		DWORD dwMRNCode = GetApp()->GetCurSN();

		BOOL bMRN = FALSE;
		WRITE_WA_MORE_TIMES(bMRN, pBTMan->cmdWriteMRNCode(nIndex, dwMRNCode));
		
		if(!bMRN)
		{
			GYMsgBox(IDS_PAT_WRITE_MRNCODE_ERROR, MB_ICONSTOP);
			return FALSE;
		}
	}

	m_BasicPara = wbp;	// update the basic parameters
	return TRUE;
}

BOOL CFirstDiagPage::SaveDiagRecord()
{
	CString strWADevID;
	CWnd* pWADevID = GetDlgItem(IDC_STATIC_WA_ID);
	pWADevID->GetWindowText(strWADevID);

	//  Also save BlueTooth address
	CWAManager* pBTMan = GetApp()->GetWAMan();
	int nIndex = GetApp()->GetCurrentWAIndex();
	CString strBTAddress = pBTMan->GetWAAddress(nIndex);
	
	CString strPulseFreq;
	CWnd* pPulseFreq = GetDlgItem(IDC_EDIT_STI_RATE);
	pPulseFreq->GetWindowText(strPulseFreq);

	CString strPulseWidth;
	CWnd* pPulseWidth = GetDlgItem(IDC_EDIT_STI_BANDWIDTH);
	pPulseWidth->GetWindowText(strPulseWidth);

	CString strPulseCount;
	CWnd* pPulseCount = GetDlgItem(IDC_EDIT_SAFE_PULSENUM);
	pPulseCount->GetWindowText(strPulseCount);
	
	CString strUpTime;
	CWnd* pUpTime = GetDlgItem(IDC_EDIT_UP_TIME);
	pUpTime->GetWindowText(strUpTime);
	
	CString strDownTime;
	CWnd* pDownTime = GetDlgItem(IDC_EDIT_DOWN_TIME);
	pDownTime->GetWindowText(strDownTime);
	
	CString strStiOpen;
	CWnd* pStiOpen = GetDlgItem(IDC_EDIT_STI_OPEN);
	pStiOpen->GetWindowText(strStiOpen);
	
	CString strStiClose;
	CWnd* pStiClose = GetDlgItem(IDC_EDIT_STI_CLOSE);
	pStiClose->GetWindowText(strStiClose);
	
	double dUpTime = _tcstod(strUpTime, NULL);
	double dDownTime = _tcstod(strDownTime, NULL);
	DWORD dwStiClose = _ttoi(strStiClose);
	DWORD dwStiOpen = _ttoi(strStiOpen);

	CString strMRN = GetApp()->GetCurMRN();
	CString strRealName = GetApp()->GetUserName();

	CString strExist;
	strExist.Format(_T("SELECT COUNT(*) AS RecCnt FROM DiagRecord WHERE MedicalRecordNo='%s' AND DiagSN=0"), strMRN);
	CGYDataSet setDiagRec(strExist);
	
	BOOL bExist = FALSE;
	if(!setDiagRec.IsEOF())
	{
		CString strRecCnt;
		setDiagRec.GetFieldValue(_T("RecCnt"), strRecCnt);
		if(strRecCnt != _T("0"))
			bExist = TRUE;
	}

	CTime curTime = CTime::GetCurrentTime();
	CString strCurTime;
	strCurTime.Format(_T("%4d-%02d-%02d %02d:%02d:%02d"),	curTime.GetYear(),	curTime.GetMonth(),		curTime.GetDay(),
															curTime.GetHour(),	curTime.GetMinute(),	curTime.GetSecond());


	CString strSQL;
	if(bExist)
	{
		strSQL.Format(_T("UPDATE DiagRecord SET DiagDate='%s', UserName='%s', IsDraft=0, UpTime=%.1f, DownTime=%.1f, StiOpen=%d, StiClose=%d, PulseFreq=%s, PulseWidth=%s, PulseCount=%s, WADevID='%s', WAAddress='%s' WHERE MedicalRecordNo='%s' AND DiagSN=0"),
												strCurTime,		strRealName,				dUpTime,		dDownTime,	dwStiOpen, dwStiClose,	strPulseFreq, strPulseWidth, strPulseCount,	strWADevID,	strBTAddress,					strMRN);
	}
	else
	{
		strSQL.Format(_T("INSERT INTO DiagRecord (MedicalRecordNo, DiagSN, DiagDate, UserName, IsDraft, UpTime, DownTime, StiOpen, StiClose, PulseFreq, PulseWidth, PulseCount, WADevID, WAAddress) VALUES ('%s', 0, '%s', '%s', 0, %.1f, %.1f, %d, %d, %s, %s, %s, '%s', '%s')"),
												strMRN, strCurTime, strRealName, dUpTime, dDownTime, dwStiOpen, dwStiClose, strPulseFreq, strPulseWidth, strPulseCount,	strWADevID, strBTAddress);
	}

	CString strSQL2;
	strSQL2.Format(_T("UPDATE PatientInfo SET DiagProgress=%d WHERE MedicalRecordNo='%s'"), TAB_INDEX_1STDIAG, strMRN);
	
	if(!setDiagRec.ExecuteSQL(strSQL) || !setDiagRec.ExecuteSQL(strSQL2))
	{
		DataErrorMsgBox(__FILE__, __LINE__, strSQL + _T("\r\n\r\n") + strSQL2, setDiagRec.GetLastErrorString());
		return FALSE;
	}

	return TRUE;
}

BOOL CFirstDiagPage::ShowTableData(CString strMRN)
{
	CString strSQL;
	strSQL.Format(_T("SELECT DiagDate, UserName, UpTime, DownTime, StiOpen, StiClose, PulseFreq, PulseWidth, PulseCount, WADevID, WAAddress FROM DiagRecord WHERE MedicalRecordNo='%s' AND DiagSN=0"), strMRN);

	CGYDataSet setDiagRec(strSQL);
	if(setDiagRec.IsEOF())
	{
		SetTimer(GYWA_MSG_1STDIAG_TIMERID_SHOWGAIT, 100, ShowGaitTimerFunc);
		return FALSE;
	}

	CString strFreq;
	setDiagRec.GetFieldValue(_T("PulseFreq"), strFreq);
	GetDlgItem(IDC_EDIT_STI_RATE)->SetWindowText(strFreq);
	
	CString strWidth;
	setDiagRec.GetFieldValue(_T("PulseWidth"), strWidth);
	GetDlgItem(IDC_EDIT_STI_BANDWIDTH)->SetWindowText(strWidth);
	
	CString strCount;
	setDiagRec.GetFieldValue(_T("PulseCount"), strCount);
	GetDlgItem(IDC_EDIT_SAFE_PULSENUM)->SetWindowText(strCount);
	
	CString strDevID;
	setDiagRec.GetFieldValue(_T("WADevID"), strDevID);
	GetDlgItem(IDC_STATIC_WA_ID)->SetWindowText(strDevID);

	CString strBTAddress;
	setDiagRec.GetFieldValue(_T("WAAddress"), strBTAddress);
	
	CString strUpTime;
	setDiagRec.GetFieldValue(_T("UpTime"), strUpTime);
	GetDlgItem(IDC_EDIT_UP_TIME)->SetWindowText(strUpTime);
	GetDlgItem(IDC_EDIT_UP_TIME)->EnableWindow(FALSE);
	
	CString strDownTime;
	setDiagRec.GetFieldValue(_T("DownTime"), strDownTime);
	GetDlgItem(IDC_EDIT_DOWN_TIME)->SetWindowText(strDownTime);
	GetDlgItem(IDC_EDIT_DOWN_TIME)->EnableWindow(FALSE);
	
	CString strTriggerOpen;
	setDiagRec.GetFieldValue(_T("StiOpen"), strTriggerOpen);
	GetDlgItem(IDC_EDIT_STI_OPEN)->SetWindowText(strTriggerOpen);
	GetDlgItem(IDC_EDIT_STI_OPEN)->EnableWindow(FALSE);
	
	CString strTriggerClose;
	setDiagRec.GetFieldValue(_T("StiClose"), strTriggerClose);
	GetDlgItem(IDC_EDIT_STI_CLOSE)->SetWindowText(strTriggerClose);	
	GetDlgItem(IDC_EDIT_STI_CLOSE)->EnableWindow(FALSE);

	GetDlgItem(IDC_STATIC_CONNECT_STATE)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_STATIC_FIRMWARE_VER)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_STATIC_FWVER)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_STATIC_CONNSTATE)->ShowWindow(SW_HIDE);


	GetDlgItem(IDC_RADIO_MAN)->EnableWindow(FALSE);
	GetDlgItem(IDC_RADIO_AUTO)->EnableWindow(FALSE);

	GetDlgItem(IDB_WRITEPARAM)->EnableWindow(FALSE);
	
	// Make prepare() not to read param from WA and init UIs again 
	m_BasicPara.dwSize = sizeof(m_BasicPara);

	//  refresh gait in first diagnose
	SetTimer(GYWA_MSG_1STDIAG_TIMERID_SHOWGAIT, 100, ShowGaitTimerFunc);

	return TRUE;
}

BOOL CFirstDiagPage::EnableInputControls(BOOL bEnable)
{
	GetDlgItem(IDC_EDIT_STI_OPEN)->EnableWindow(bEnable);
	GetDlgItem(IDC_EDIT_STI_CLOSE)->EnableWindow(bEnable);

	GetDlgItem(IDC_RADIO_MAN)->EnableWindow(bEnable);
	GetDlgItem(IDC_RADIO_AUTO)->EnableWindow(bEnable);
	
	GetDlgItem(IDC_BUTTON_SNAP)->EnableWindow(bEnable);
	GetDlgItem(IDC_BUTTON_CONTINUE)->EnableWindow(FALSE); // In this function, it will be always FALSE
	GetDlgItem(IDB_WRITEPARAM)->EnableWindow(bEnable);

	GetDlgItem(IDC_SPIN_OPEN_VALUE)->EnableWindow(bEnable);
	GetDlgItem(IDC_SPIN_CLOSE_VALUE)->EnableWindow(bEnable);
	
	CPageDialog::EnableInputControls(bEnable);
	return TRUE;
}

void CFirstDiagPage::OnButtonSnap() 
{
	DWORD dwMRNCode = GetApp()->GetCurSN();

	if(m_pGaitDraw)
	{
		m_pGaitDraw->GetGaitSnap(m_adwGaitSnap, &m_nStartPos, &m_dwStiOpen, &m_dwStiClose);
		m_pGaitDraw->SnapGait(dwMRNCode, 0);
		m_pGaitDraw->PauseDraw(TRUE);
		
		GetDlgItem(IDC_BUTTON_CONTINUE)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_SNAP)->EnableWindow(FALSE);
	}
}

void CFirstDiagPage::ShowGaitTimerFunc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
	CFirstDiagPage* pThis = (CFirstDiagPage*)CFirstDiagPage::FromHandle(hWnd);
	if(pThis)
	{
		pThis->KillTimer(nIDEvent);

		if(pThis->IsControlsEnable())
		{
			//  if sdiag is working, stop the gait first
			CPatMan* pParent = (CPatMan*)pThis->GetParent();
			if(pParent)
			{
				CSubsqtDiagPage* pSDiag = (CSubsqtDiagPage*)pParent->GetTabPage(TAB_INDEX_SUBSDIAG);
				if(pSDiag)
					pSDiag->ClearUp();
			}

			//  it's a new 1st diagnose
			if(pThis->m_pGaitDraw == NULL)
			{
				//  after gait started, the basic param is not able to been read, so read first.
				memset(&pThis->m_BasicPara, 0, sizeof(pThis->m_BasicPara));
				pThis->GetBasicParamToDisplay();

				//  from a query, new a Gait
				CWnd* pGaitLabel = pThis->GetDlgItem(IDC_STATIC_1STDIAG_GAIT);
				pThis->m_pGaitDraw = new CGaitAnaly(pGaitLabel, GetApp()->GetCurrentWAIndex());
				pThis->m_pGaitDraw->SetGaitPara(&pThis->m_BasicPara);
				pThis->m_pGaitDraw->StartDraw();

				pThis->GetDlgItem(IDC_STATIC_CONNECT_STATE)->ShowWindow(SW_SHOW);
				pThis->GetDlgItem(IDC_STATIC_FIRMWARE_VER)->ShowWindow(SW_SHOW);
				pThis->GetDlgItem(IDC_STATIC_FWVER)->ShowWindow(SW_SHOW);
				pThis->GetDlgItem(IDC_STATIC_CONNSTATE)->ShowWindow(SW_SHOW);
			}
			else
			{
				//  paused
				if(pThis->m_pGaitDraw->IsDrawPaused())
					pThis->DrawPausedStaticGait();
				else
				{
					// do nothing
				}
			}
		}
		else
		{
			pThis->StopGait();

			if(pThis->m_pGaitDraw == NULL)
				CGaitAnaly::ShowStaticGaitSnap(pThis->GetDlgItem(IDC_STATIC_1STDIAG_GAIT), 0);
		}
	}
}

void CFirstDiagPage::OnRefreshOpenClose(WPARAM wParam, LPARAM lParam)
{
	CButton* pParamAuto = (CButton*)GetDlgItem(IDC_RADIO_AUTO);
	ASSERT(pParamAuto);

	if(pParamAuto->GetCheck())
	{
		m_bTextChangeFromGait = TRUE;

		CString strTriggerOpen;
		strTriggerOpen.Format(_T("%d"), wParam);
		GetDlgItem(IDC_EDIT_STI_OPEN)->SetWindowText(strTriggerOpen);
		
		CString strTriggerClose;
		strTriggerClose.Format(_T("%d"), lParam);
		GetDlgItem(IDC_EDIT_STI_CLOSE)->SetWindowText(strTriggerClose);

		if(m_pGaitDraw)
		{
			WA_BASIC_PARA wbp = m_pGaitDraw->GetGaitPara();
			wbp.dwOpenThr = wParam;
			wbp.dwCloseThr= lParam;

			m_pGaitDraw->SetGaitPara(&wbp);
			
			DrawPausedStaticGait();
		}
	}
}

void CFirstDiagPage::OnDeltaposSpinCloseValue(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
	
	if(pNMUpDown)
	{
		CString strOpenValue;
		CWnd* pOpenEdit = GetDlgItem(IDC_EDIT_STI_OPEN);
		ASSERT(pOpenEdit);
		
		CString strCloseValue;
		CWnd* pCloseEdit = GetDlgItem(IDC_EDIT_STI_CLOSE);
		ASSERT(pCloseEdit);

		pOpenEdit->GetWindowText(strOpenValue);
		pCloseEdit->GetWindowText(strCloseValue);
		
		int nOpenValue = _ttoi(strOpenValue);
		int nCloseValue = _ttoi(strCloseValue);
		
		if(pNMUpDown->iDelta > 0)
		{
			nCloseValue++;
			if(nCloseValue > MAX_OPEN_CLOSE_VALUE)
				nCloseValue = MAX_OPEN_CLOSE_VALUE;

#ifdef THIGH_WA_MODE
#else
			if(nCloseValue >= (nOpenValue - 1))
				nCloseValue = (nOpenValue - 1);
#endif
		}
		else if(pNMUpDown->iDelta < 0)
		{
			nCloseValue--;
			if(nCloseValue < MIN_OPEN_CLOSE_VALUE)
				nCloseValue = MIN_OPEN_CLOSE_VALUE;

#ifdef THIGH_WA_MODE
			if(nCloseValue <= (nOpenValue + 1))
				nCloseValue = (nOpenValue + 1);
#endif
		}
		
		strCloseValue.Format(_T("%d"), nCloseValue);
		pCloseEdit->SetWindowText(strCloseValue);

		OnChangeEditStiClose();
	}
	
	*pResult = 0;
}

void CFirstDiagPage::OnDeltaposSpinOpenValue(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	if(pNMUpDown)
	{
		CString strOpenValue;
		CWnd* pOpenEdit = GetDlgItem(IDC_EDIT_STI_OPEN);
		ASSERT(pOpenEdit);
		
		CString strCloseValue;
		CWnd* pCloseEdit = GetDlgItem(IDC_EDIT_STI_CLOSE);
		ASSERT(pCloseEdit);
		
		pOpenEdit->GetWindowText(strOpenValue);
		pCloseEdit->GetWindowText(strCloseValue);
		
		int nOpenValue = _ttoi(strOpenValue);
		int nCloseValue = _ttoi(strCloseValue);

		if(pNMUpDown->iDelta > 0)
		{
			nOpenValue++;
			if(nOpenValue > MAX_OPEN_CLOSE_VALUE)
				nOpenValue = MAX_OPEN_CLOSE_VALUE;

#ifdef THIGH_WA_MODE
			if(nOpenValue >= (nCloseValue - 1))
				nOpenValue = (nCloseValue - 1);
#endif
		}
		else if(pNMUpDown->iDelta < 0)
		{
			nOpenValue--;
			if(nOpenValue < MIN_OPEN_CLOSE_VALUE)
				nOpenValue = MIN_OPEN_CLOSE_VALUE;

#ifdef THIGH_WA_MODE
#else
			if(nOpenValue <= (nCloseValue + 1))
				nOpenValue = (nCloseValue + 1);
#endif
		}

		strOpenValue.Format(_T("%d"), nOpenValue);
		pOpenEdit->SetWindowText(strOpenValue);

		OnChangeEditStiOpen();
	}
	
	*pResult = 0;
}

void CFirstDiagPage::OnButtonContinue() 
{
	if(m_pGaitDraw)
	{
		m_pGaitDraw->PauseDraw(FALSE);
	}
	
	GetDlgItem(IDC_BUTTON_CONTINUE)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_SNAP)->EnableWindow(TRUE);
}

void CFirstDiagPage::OnChangeEditStiOpen() 
{
	if(m_bTextChangeFromGait)
	{
		m_bTextChangeFromGait = FALSE;
		return;
	}

	CString strOpenValue;
	GetDlgItem(IDC_EDIT_STI_OPEN)->GetWindowText(strOpenValue);
	
	int nOpenValue = _ttoi(strOpenValue);
	if(m_pGaitDraw && IsNumInRange(nOpenValue, MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE))
	{
		WA_BASIC_PARA wbp = m_pGaitDraw->GetGaitPara();
		
		wbp.dwOpenThr = nOpenValue;
		m_pGaitDraw->SetGaitPara(&wbp);

		DrawPausedStaticGait();
	}
}

void CFirstDiagPage::OnChangeEditStiClose() 
{
	if(m_bTextChangeFromGait)
	{
		m_bTextChangeFromGait = FALSE;
		return;
	}

	CString strCloseValue;
	GetDlgItem(IDC_EDIT_STI_CLOSE)->GetWindowText(strCloseValue);
	
	int nCloseValue = _ttoi(strCloseValue);
	if(m_pGaitDraw && IsNumInRange(nCloseValue, MIN_OPEN_CLOSE_VALUE, MAX_OPEN_CLOSE_VALUE))
	{
		WA_BASIC_PARA wbp = m_pGaitDraw->GetGaitPara();
		
		wbp.dwCloseThr = nCloseValue;
		m_pGaitDraw->SetGaitPara(&wbp);

		DrawPausedStaticGait();
	}
}

BOOL CFirstDiagPage::DrawPausedStaticGait()
{
	if(m_pGaitDraw && m_pGaitDraw->IsDrawPaused())
	{
		CString strOpenValue;
		CWnd* pOpenEdit = GetDlgItem(IDC_EDIT_STI_OPEN);
		ASSERT(pOpenEdit);
		
		pOpenEdit->GetWindowText(strOpenValue);
		m_dwStiOpen = _ttoi(strOpenValue);
		
		CString strCloseValue;
		CWnd* pCloseEdit = GetDlgItem(IDC_EDIT_STI_CLOSE);
		ASSERT(pCloseEdit);
		
		pCloseEdit->GetWindowText(strCloseValue);
		m_dwStiClose = _ttoi(strCloseValue);
		
		CWAManager* pBTMan = GetApp()->GetWAMan();
		int nIndex = GetApp()->GetCurrentWAIndex();
		CString strWAID = pBTMan->GetWADevID(nIndex);
		CString strDevAddress = pBTMan->GetWAAddress(nIndex);

		CWnd* pGaitLabel = GetDlgItem(IDC_STATIC_1STDIAG_GAIT);
		CGaitAnaly::ShowGaitInCtrl(pGaitLabel, m_adwGaitSnap, m_nStartPos, m_dwStiOpen, m_dwStiClose, strWAID, strDevAddress);

		return TRUE;
	}

	return FALSE;
}

BOOL CFirstDiagPage::GetBasicParamToDisplay()
{
	if(m_BasicPara.dwSize != sizeof(WA_BASIC_PARA))
	{
		CWAManager* pBTMan = GetApp()->GetWAMan();
		int nIndex = GetApp()->GetCurrentWAIndex();
		
		for(int i = 0; i < 3 && m_BasicPara.dwSize != sizeof(WA_BASIC_PARA); i++)
		{
			m_BasicPara = pBTMan->cmdGetBasicPara(nIndex);
			
			if(i < 2)
				Sleep(500);
		}
		
		if(	m_BasicPara.dwSize != sizeof(WA_BASIC_PARA) ||
			m_BasicPara.dwPulseFrq == 0					||
			m_BasicPara.dwPulseWidth == 0				||
			m_BasicPara.dwPulseCount == 0				)
		{
			pBTMan->RemoveDeviceFromList(nIndex);
			
			GYMsgBox(IDS_PAT_READ_PARAM_FAIL, MB_OK|MB_ICONSTOP);
			
			CPatMan* pParent = (CPatMan*)GetParent();
			if(pParent)
			{
				pParent->QuitPatMan();
				pBTMan->SearchWAImmediately();
			}
			
			return FALSE;
		}
		
		if(m_BasicPara.dwPulseFrq < 3 || m_BasicPara.dwPulseFrq > 6)
		{
			if(m_BasicPara.dwPulseFrq == 0)
				m_BasicPara.dwPulseFrq = 1;
			
			CString strFormat = GetResString(IDS_SYS_PULSE_FREQ_WRONG);
			CString strInfo;
			strInfo.Format(strFormat, 100.0/m_BasicPara.dwPulseFrq);
			GYMsgBox(strInfo, MB_ICONWARNING);
		}
		
		if(m_BasicPara.dwPulseWidth < 1 || m_BasicPara.dwPulseWidth > 5)
		{
			CString strFormat = GetResString(IDS_SYS_PULSE_WIDTH_WRONG);
			CString strInfo;
			strInfo.Format(strFormat, m_BasicPara.dwPulseWidth * 50);
			GYMsgBox(strInfo, MB_ICONWARNING);
		}		
		
		
		CString strFreq;
		strFreq.Format(_T("%.1f"), 100.0/m_BasicPara.dwPulseFrq);
		
		CEdit* pStiRate = (CEdit*)GetDlgItem(IDC_EDIT_STI_RATE);
		pStiRate->SetWindowText(strFreq);
		
		CString strWidth;
		strWidth.Format(_T("%d"), m_BasicPara.dwPulseWidth * 50);
		
		CEdit* pBanDWidth = (CEdit*)GetDlgItem(IDC_EDIT_STI_BANDWIDTH);
		pBanDWidth->SetWindowText(strWidth);
		
		CString strCount;
		strCount.Format(_T("%d"), m_BasicPara.dwPulseCount);
		CEdit* pPulesNum = (CEdit*)GetDlgItem(IDC_EDIT_SAFE_PULSENUM);
		pPulesNum->SetWindowText(strCount);
		
		
		CString strWAID = pBTMan->GetWADevID(nIndex);
		GetDlgItem(IDC_STATIC_WA_ID)->SetWindowText(strWAID);
		
		CString strFMVer = pBTMan->cmdReadFirmwareVer(nIndex);
		GetDlgItem(IDC_STATIC_FIRMWARE_VER)->SetWindowText(strFMVer);
		
		CString strUpTime;
		strUpTime.Format(_T("%.1f"), m_BasicPara.dwStiUpTime / 100.0);
		GetDlgItem(IDC_EDIT_UP_TIME)->SetWindowText(strUpTime);
		
		CString strDownTime;
		strDownTime.Format(_T("%.1f"), m_BasicPara.dwStiDownTime / 100.0);
		GetDlgItem(IDC_EDIT_DOWN_TIME)->SetWindowText(strDownTime);
		
		CString strTriggerOpen;
		strTriggerOpen.Format(_T("%d"), m_BasicPara.dwOpenThr);
		GetDlgItem(IDC_EDIT_STI_OPEN)->SetWindowText(strTriggerOpen);
		
		CString strTriggerClose;
		strTriggerClose.Format(_T("%d"), m_BasicPara.dwCloseThr);
		GetDlgItem(IDC_EDIT_STI_CLOSE)->SetWindowText(strTriggerClose);
	}

	return TRUE;
}
